<?php
defined('BY_JYA') or exit('Access Denied');
if($do=='getbuyed'){
	$installed=$_RQ['installed'];
	if($installed){
		//兼容新的提交机制
		$installed=explode(';',$installed);
	}
	$plugins=pdo_getall('core_trade',array('uniacid'=>$_SESSION['uniacid'],'type'=>10,'status'=>1),array('pid'),'pid');
	if(!empty($plugins)){
		$plugins=array_keys($plugins);
		if($installed){
			$plugins=array_diff($plugins,$installed);
		}
		if($plugins){
			$plugins=pdo_getall('cloud_manage_template',array('status'=>1,'show'=>1,'id'=>$plugins),array('name','title','version'));
		}else{
			$plugins=array();
		}
	}
	eci($plugins);
}
if($do=='getup'){
	$ids=$_RQ['names'];
	if(!$ids){
		eci('页面参数错误！1','error');
	}
	
	$plugins=pdo_getall('cloud_manage_template',array('name'=>$ids),array('name','release','version'),'name');
	
	eci($plugins);
}

if($do=='install'){
	$name=trim($_RQ['name']);
	if(!$name){
		eci('请先选择要安装的模版！','error');
	}
	$id=pdo_getcolumn('cloud_manage_template',array('name'=>$name),'id');
	if(!$id){
		eci('指定模版不存在！','error');
	}
	check_buy($id,10);
	$res=array();
	$template=pdo_get('cloud_manage_template',array('id'=>$id),array('id','name','title','version','release','diy_form'));
	$zipname=MANAGE_TEMPLATE_ZIP.$template['name'].'.zip';
	$fsize=filesize($zipname);
	$size=204800;
	$parts=intval(ceil($fsize/$size));
	$download=array(
			'uniacid'=>$_SESSION['uniacid'],
			'type'=>2,
			'size'=>$size,
			'parts'=>$parts,
			'createtime'=>TIMESTAMP,
			'zipfile'=>str_replace(ROOT_AT,'',$zipname),
			'remark'=>"安装模版【{$template['title']}（{$template['name']}）】",
	);
	pdo_insert('core_cloud_download',$download);
	$id=pdo_insertid();
	$res['files']=array('id'=>$id,'parts'=>$parts);
	$res['install']=array_fields(array('id','name','title','version','release','diy_form'),$template);
	eci($res);
}

if($do=='build'){
	$name=trim($_RQ['name']);
	if(!$name){
		eci('请先选择要更新的应用！','error');
	}
	$id=pdo_getcolumn('cloud_manage_template',array('name'=>$name),'id');
	if(!$id){
		eci('指定模版不存在！','error');
	}
	check_buy($id,10);
	
	$return=array();
	//获取模版信息
	$plugin=pdo_get('cloud_manage_template',array('id'=>$id));
	//整理文件
	$return['files']=array();
	$module_files=cfc('files')->file_tree(MANAGE_TEMPLATE_FILE.$plugin['name'],array('/manifest.xml'));
	foreach($module_files as $file){
		$md5=md5_file(MANAGE_TEMPLATE_FILE.$plugin['name'].$file);
		$return['files'][]=array('path'=>$file,'checksum'=>$md5);
	}
	if($_RQ['only_file']){
		//只需文件数据，直接返回
		eci($return);
	}
	$return['update']=array_fields(array('title','version','release','diy_form'),$plugin);
	eci($return);
}
if($do=='getdata'){
	$name=intval($_RQ['name']);
	if(!$name){
		eci('请先选择要更新的应用！1','error');
	}
	$id=pdo_getcolumn('cloud_manage_template',array('name'=>$name),'id');
	if(!$id){
		eci('指定模版不存在！','error');
	}
	check_buy($id,10);
	$return=array();
	//获取模版信息
	$plugin=pdo_get('cloud_manage_template',array('id'=>$id),array('name','title'));
	if(!$plugin['name']){
		eci('模板不存在！','error');
	}
	$files=$_RQ['files'];
	$zip = new ZipArchive();
	$zipname=cfc('files')->get_source_name('data','data');
    $zip->open(ROOT_AT.$zipname, ZipArchive::CREATE);
	foreach($files as $file){
		if(!empty($file)){
			$fname = MANAGE_TEMPLATE_FILE.$plugin['name']. $file;
			if(is_file($fname)){
				$zip->addFile($fname,$file);
			}
			
		}
	}
	$zip->close();
		//采用新方法下载更新数据
		$filesize=filesize(ROOT_AT.$zipname);
		$size=204800;
		$parts=ceil($filesize/$size);
		$parts=intval($parts);
		$download=array(
			'uniacid'=>$_SESSION['uniacid'],
			'type'=>4,
			'size'=>$size,
			'parts'=>$parts,
			'createtime'=>TIMESTAMP,
			'zipfile'=>$zipname,
			'remark'=>"更新模版【{$plugin['title']}（{$plugin['name']}）】",
		);
		pdo_insert('core_cloud_download',$download);
	$id=pdo_insertid();
	$return=array('id'=>$id,'parts'=>$parts);
	eci($return);

}
